import { ESGeoWater, ESJEditingMode } from "earthsdk3";
import { ElMessage } from "element-plus";
import { SceneObjectKey, UniteChanged, Vector, bind, extendClassProps, reactJsonWithUndefined } from "earthsdk3";

/**
 * 测试 水面修补
 */
export class ESMsWater extends ESGeoWater {

    static override readonly createDefaultProps = () => ({
        ...ESGeoWater.createDefaultProps(),
        textureSize: undefined as string | undefined,
        textureId: undefined as string | undefined,
        srcGeom: reactJsonWithUndefined<{ [xx: string]: any }>(undefined),
    });
    static override readonly type: string = this.register('ESMsWater', this, { chsName: '水面修补', tags: ['ESObjects', '_ES_Impl_Cesium', '_ES_Impl_UE'], description: "地理多边形体" });

    override get json() {
        return {
            // @ts-ignore
            ...this._innerGetJson(),
            // @ts-ignore
            "textureSize": this.textureSize,
            // @ts-ignore
            "textureId": this.textureId,
        } as any;
    }
       /**
     * 根据 JSON 数据设置对象的属性。
     * @param value - JSON 数据。
     */
    set json(value: any) { this._innerSetJson(value) }

    override updateEditing() {
        const mode = [
            ESJEditingMode.CircularAppend,
            ESJEditingMode.Translation,
            ESJEditingMode.CircularInsert
        ]
        if (this.points && this.points.length > 2) {
            mode.reverse();
        }
        this.activeViewer?.startEditing(this, mode, { callSource: 'inner' });
    }

    private _eSGeoWater = this.dv(new ESGeoWater());
    get eSGeoWater() { return this._eSGeoWater; }
    constructor(id?: SceneObjectKey) {
        super(id);
        this.components.add(this._eSGeoWater);
        this.ad(() => {
            this.components.delete(this._eSGeoWater);
        })
        const eSGeoWater = this._eSGeoWater;
        //属性同步
        {
            this.d(bind([eSGeoWater, 'waterType'], [this, 'waterType']));
            this.d(bind([eSGeoWater, 'points'], [this, 'points']));
            this.d(bind([eSGeoWater, 'frequency'], [this, 'frequency']));
            this.d(bind([eSGeoWater, 'waveVelocity'], [this, 'waveVelocity']));
            this.d(bind([eSGeoWater, 'amplitude'], [this, 'amplitude']))
            this.d(bind([eSGeoWater, 'specularIntensity'], [this, 'specularIntensity']));
            this.d(bind([eSGeoWater, 'flowDirection'], [this, 'flowDirection']));
            this.d(bind([eSGeoWater, 'waterColor'], [this, 'waterColor']));
            this.d(bind([eSGeoWater, 'waterImage'], [this, 'waterImage']));
            this.d(bind([eSGeoWater, 'show'], [this, 'show']));
        }
        {
            const update = () => {
                //经纬度 转 Geojson
                const ArrayAdd = (arr: Iterable<unknown> | null | undefined) => {
                    let a = [...new Set(arr)] as [number, number, number][]; //去重加首尾相同
                    if (!Vector.equals(a[0], a[a.length - 1])) a.push(a[0]);
                    return [...a];
                }
                let GeoJsonArr = this.points;
                if (!GeoJsonArr || GeoJsonArr.length < 3) return;
                let tempPos = [];
                for (let index = 0; index < GeoJsonArr.length; index++) {
                    let el: any = GeoJsonArr[index];
                    tempPos.push([el[0], el[1], el[2] ?? 0]);
                }
                // @ts-ignore
                this.srcGeom = {
                    type: "Polygon",
                    coordinates: [ArrayAdd(tempPos)],
                };
            }
            update();
            this.ad(this.pointsChanged.don(update));
        }
        // this.show = false
        {
            let message: any
            const updata = () => {
                if (this.editing) {
                    message = ElMessage({
                        message: '1. 双击鼠标左键或点击ESC键退出编辑2. 点击空格键进行编辑方式的切换',
                        duration: 0,
                        plain: true,
                    })
                } else {
                    message && message.close()
                }
            }
            this.dispose(this.editingChanged.disposableOn(updata))
            this.dispose(() => message && message.close())
        }
    }
}

export interface ESGeoExtrudedPolygonUFMS extends UniteChanged<ReturnType<typeof ESGeoWater.createDefaultProps>> { }
